package com.cdm.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JFileChooser;

import org.apache.commons.io.Charsets;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cdm.utils.CSVUtils;
import com.cdm.utils.CsvReader;

@Controller
public class webTest {
	
	
	@RequestMapping("/getContentByHtml")
    @ResponseBody
    public void getContentByHtml(){
		String keyword = "整容";
		String url = "http://www.baidu.com/s?pn=0&wd="+keyword;
		Document doc = null;
		try {
			doc = Jsoup.connect(url).get();
			Element div = doc.getElementById("content_left");
			System.out.println("all="+div.toString());
			Element link = doc.select("font").first();//查找第一个a元素
			//System.out.println("all="+link.toString());
			//System.out.println("text="+link.text().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("doc="+div);
	}

	/**
     * CSV导出
	 * @throws Exception 
     * 
     * @throws Exception
     */
    @RequestMapping("/exportCsv")
    @ResponseBody
    public void exportCsv(HttpServletResponse response) throws Exception {
        List<String> dataList=new ArrayList<String>();
        dataList.add("1,张三,男");
        dataList.add("2,李四,男");
        dataList.add("3,小红,女");
        for (int i = 0; i < 30; i++) {
			dataList.add(i+",小明"+i+",男");
		}
        boolean isSuccess=CSVUtils.exportCsv(new File("D:/@mylike/interfaceDoc/cvstest/ljq.csv"), dataList);
        System.out.println("/exportCsv="+isSuccess);
    }
    
    /**
     * CSV导出
     * 
     * @throws Exception
     */
    @RequestMapping("/importCsv")
    @ResponseBody
    public void importCsv()  {
    	
    	try {
			CsvReader csvReader = new CsvReader("D:/csv/1491526805419.csv", '\t', Charsets.UTF_8);
			String[] str = csvReader.getValues();
			//System.out.println("sa=="+str[0].toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	
    	
    	
        /*List<String> dataList=CSVUtils.importCsv(new File("D:/@mylike/interfaceDoc/cvstest/test.csv"));
        String[] datas ;
        int sucess = 0;
        int failure = 0;
        if(dataList!=null && !dataList.isEmpty()){
            for(String data : dataList){
                System.out.println(data);
            	datas = data.split(",");
            	
            	if (datas.length>0) {
					sucess++;
				}else {
					failure++;
				}
            }
            System.out.println("共"+dataList.size()+"条；成功："+sucess+"；失败："+failure);
        }*/
    }
    
	
}
